package com.lucky.back.controller;

import com.github.xiaoymin.knife4j.annotations.ApiOperationSupport;
import com.lucky.back.pojo.dto.ProductAddNewDTO;
import com.lucky.back.pojo.entity.Product;
import com.lucky.back.pojo.entity.Users;
import com.lucky.back.pojo.vo.BannerListItemVO;
import com.lucky.back.pojo.vo.ProductListItemVO;
import com.lucky.back.pojo.vo.ProductStandardVO;
import com.lucky.back.service.IProductService;
import com.lucky.back.web.JsonResult;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpSession;
import java.util.List;

@Slf4j
@Api(tags = "02.商品管理模块")
@RestController
@RequestMapping("/product")
public class ProductController {

    @Autowired
    IProductService productService;

    @ApiOperation("查询商品")
    @ApiOperationSupport(order = 420)
    @GetMapping("")
    public JsonResult list(){
        List<ProductListItemVO> list = productService.list();
        JsonResult jsonResult = JsonResult.ok();
        jsonResult.setData(list);
        return jsonResult;
    }

    @ApiOperation("按classID查询商品")
    @ApiOperationSupport(order = 420)
    @GetMapping("/listByclass/{id}")
    public JsonResult listByclassId(@PathVariable Long id){
        List<ProductListItemVO> list = productService.listByClassId(id);
        JsonResult jsonResult = JsonResult.ok();
        jsonResult.setData(list);
        return jsonResult;
    }

    @ApiOperation("添加商品")
    @ApiOperationSupport(order = 100)
    @PostMapping("/add-new")
    public JsonResult addNew(@Validated ProductAddNewDTO productAddNewDTO){
        System.out.println("conlltroller收到请求");
        productService.addNew(productAddNewDTO);
        System.out.println("到达controller");
        System.out.println(productAddNewDTO);
        return JsonResult.ok();
    }

    // http://localhost:9080/brands/9527
    @ApiOperation("根据id查询详情")
    @ApiOperationSupport(order = 400)
    @GetMapping("/{id:[0-9]+}")
    public JsonResult<ProductStandardVO> getStandardById(@PathVariable Long id) {
        log.debug("开始处理【根据id查询品牌详情】的请求，参数：{}", id);
        ProductStandardVO brand = productService.getStandardById(id);
        return JsonResult.ok(brand);
    }

    @ApiOperation("修改商品")
    @ApiOperationSupport(order = 110)
    @PostMapping("/update")
    public JsonResult update(@Validated Product product){
        System.out.println("conlltroller收到请求");
        productService.updateById(product);
        return JsonResult.ok();
    }
    @ApiOperation("搜索商品")
    @ApiOperationSupport(order = 120)
    @GetMapping("/listByProduct/{keyword}")
    public JsonResult<List<ProductListItemVO>> listByProduct(@PathVariable String keyword){
        System.out.println("conlltroller收到请求");
        System.out.println(keyword);
        List<ProductListItemVO>list=productService.listByProduct(keyword);
        return JsonResult.ok(list);
    }

    @ApiOperation("按价格排序")
    @ApiOperationSupport(order = 130)
    @GetMapping("/selectListPrice")
    public JsonResult<List<ProductListItemVO>> selectPrice(){
        List<ProductListItemVO> list = productService.listByPrice();
        return JsonResult.ok(list);
    }


    @ApiOperation("按销量排序")
    @ApiOperationSupport(order = 140)
    @GetMapping("/selectListSaleCount")
    public JsonResult<List<ProductListItemVO>> selectSaleCount(){
        List<ProductListItemVO> list = productService.listBySaleCount();
        return JsonResult.ok(list);
    }

}
